# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=eigen

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}3")
pkgver=3.2.1
pkgrel=1
pkgdesc="Lightweight C++ template library for vector and matrix math (mingw-w64)"
arch=('any')
url='http://eigen.tuxfamily.org'
license=('MPL2')
options=('strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-recode")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=("http://bitbucket.org/eigen/eigen/get/${pkgver}.tar.bz2"
    'eigen-3.1.2_osversion.patch'
    'eigen-3.2_gcc58087.patch'
    'TryRunResults.cmake')
md5sums=('ece1dbf64a49753218ce951624f4c487'
         '89587f79971efc8f363d478f48f86829'
         '1db055c7681799bb21118b2dcf95980a'
         '67e151de214f29c1e6cf00452916d7d4')

prepare() {
  cd "${srcdir}"/eigen-eigen-*

  patch -Np2 -i "${srcdir}/eigen-3.1.2_osversion.patch"
  patch -Np1 -i "${srcdir}/eigen-3.2_gcc58087.patch"
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  cd ${srcdir}/eigen-eigen-*
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ${MINGW_PREFIX}/bin/cmake.exe \
    -C "$srcdir"/TryRunResults.cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DEIGEN_BUILD_PKGCONFIG:BOOL=ON \
    ..

  make
}

package() {
  cd ${srcdir}/eigen-eigen-*/build-${MINGW_CHOST}

  make install
  mv ${pkgdir}${MINGW_PREFIX}/share ${pkgdir}${MINGW_PREFIX}/lib

  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/eigen3.pc
}
